//
//  TopicHelper.m
//  HomeDecoration
//
//  Created by xalo on 16/2/27.
//  Copyright © 2016年 美家项目组. All rights reserved.
//

#import "TopicHelper.h"
#import <AFNetworking/AFNetworking.h>
#import "TopicModel.h"
#import <UIKit/UIKit.h>
@interface TopicHelper ()

@property (nonatomic,strong) NSMutableArray *modelArray;

@property (nonatomic,strong) NSMutableArray  *imageArray;


@end





@implementation TopicHelper

//数据请求单例
+(TopicHelper *)sharedManager
{
    static TopicHelper *hendle = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        hendle = [[TopicHelper alloc] init];
        
    });
    
    return hendle;
}

//解析数据,刷新UI
-(void)fetchDataWithUrl:(NSString *)url block:(BLOCK)block
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        
        [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
            
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            NSArray *array = [NSArray array];
            
            array = (NSArray *)responseObject;
            
            for (NSDictionary *dic in array) {
                
                TopicModel *topicModel = [[TopicModel alloc] init];
                
                [topicModel setValuesForKeysWithDictionary:dic];
               
                [self.modelArray addObject:topicModel];
               
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                block();
                
            });            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"%@",error);
            
        }];        
    });
}


//懒加载

-(NSMutableArray *)modelArray
{
    if (!_modelArray) {
        
        _modelArray = [NSMutableArray array];
    }

    return _modelArray;
}

-(NSMutableArray *)imageArray
{
    if (!_imageArray) {
        
        _imageArray = [NSMutableArray array];
    }
    
    return _imageArray;


}



//返回数组个数

-(NSInteger)modelArrayForCount
{
    return self.modelArray.count;
}


//根据单元格下标返回model
-(TopicModel *)returnOfModelWithIndex:(NSInteger)index
{
    return self.modelArray[index];

}














@end
